package 二叉树;

import 链表.Node;

import java.util.*;

/**
 * TODO
 * <p>
 * 1、
 * <p/>
 * User: zhaixiaolong Date:2024/2/20 9:33 ProjectName: algorithm Version:
 */
public class 二叉树深度优先遍历 {

    static TreeNode initTree() {
        TreeNode root = new TreeNode(1, null, new TreeNode(2, new TreeNode(3), new TreeNode(4)));
        return root;
    }

    public static void main(String[] args) {
        TreeNode root = initTree();
        List<TreeNode> list = new ArrayList<>();
        preOrder(root, list);
        System.out.println(Arrays.toString(list.toArray()));
    }

    /**
     * 前序遍历：根->左->右
     *
     * @param root
     * @param list
     */
    public static void preOrder(TreeNode root, List<TreeNode> list) {
        if (Objects.isNull(root)) {
            return;
        }
        list.add(root);
        preOrder(root.left, list);
        preOrder(root.right, list);
    }

    /**
     * 前序遍历：左->根->右
     *
     * @param root
     * @param list
     */
    public static void mindOrder(TreeNode root, List<TreeNode> list) {
        if (Objects.isNull(root)) {
            return;
        }
        preOrder(root.left, list);
        list.add(root);
        preOrder(root.right, list);
    }

    /**
     * 前序遍历：左->根->右
     *
     * @param root
     * @param list
     */
    public static void afterOrder(TreeNode root, List<TreeNode> list) {
        if (Objects.isNull(root)) {
            return;
        }
        preOrder(root.left, list);
        preOrder(root.right, list);
        list.add(root);
    }



}
